package submarinebattle;

import javax.swing.*;
import java.awt.*;
import java.util.Random;

/** 海洋对象 */
public abstract class SeaObject extends Object {
    public static final int LIVE = 0;
    public static final int DEAD = 1;
    private int state = LIVE;

    private int width;  //宽
    private int height; //高
    private int x;      //x坐标
    private int y;      //y坐标
    private int speed;  //移动速度

    public int getState() {
        return state;
    }
    public void setState(int state) {
        this.state = state;
    }
    public int getWidth() {
        return width;
    }
    public void setWidth(int width) {
        this.width = width;
    }
    public int getHeight() {
        return height;
    }
    public void setHeight(int height) {
        this.height = height;
    }
    public int getX() {
        return x;
    }
    public void setX(int x) {
        this.x = x;
    }
    public int getY() {
        return y;
    }
    public void setY(int y) {
        this.y = y;
    }
    public int getSpeed() {
        return speed;
    }
    public void setSpeed(int speed) {
        this.speed = speed;
    }

    /** 专门给侦察潜艇、鱼雷潜艇、水雷潜艇提供的 */

    public SeaObject(int width,int height){
        this.width = width;
        this.height = height;
        x = -width; //负的潜艇的宽
        Random rand = new Random(); //随机数对象
        y = rand.nextInt(World.HEIGHT-height-150+1)+150; //150到(窗口高-潜艇高)之间的
        speed = rand.nextInt(3)+1; //1到3之间的
    }

    public SeaObject(int width,int height,int x,int y,int speed){
        this.width = width;
        this.height = height;
        this.x = x;
        this.y = y;
        this.speed = speed;
    }

    // 海洋移动 /
    public abstract void move();

    //判断是否是活着的
    public boolean isLive(){
        return state==LIVE; //若state为LIVE，表示活着的，返回true，否则返回false
    }

    // 判断是否是死了的
    public boolean isDead(){
        return state==DEAD; //若state为DEAD，表示死了的，返回true，否则返回false
    }

    // 获取对象的图片
    public abstract ImageIcon getImage();

    // 画对象 g:画笔
    public void paintImage(Graphics g){
        if(this.isLive()){
            this.getImage().paintIcon(null,g,this.x,this.y);
        }
    }

    // 检测潜艇越界
    public boolean isOutOfBounds(){
        return this.x>=World.WIDTH;
    }

    // 检测碰撞 this:一个对象 other:另一个对象
    public boolean isHit(SeaObject other){
        int x1 = this.x-other.width;
        int x2 = this.x+this.width;
        int y1 = this.y-other.height;
        int y2 = this.y+this.height;
        int x = other.x;
        int y = other.y;

        return x>=x1 && x<=x2
                &&
                y>=y1 && y<=y2; //x在x1与x2之间，并且，y在y1与y2之间，即为撞上了
    }

    public void goDead(){
        state = DEAD; //将当前状态修改为DEAD死了的
    }
}
